from pathlib import Path

import tensorflow as tf


def get_label_to_index(data_root: Path):
    label_names = sorted(item.name for item in data_root.glob('train/*') if item.is_dir())
    label_to_index = dict((name, index) for index, name in enumerate(label_names))
    return label_to_index


def get_all_image_paths(data_root: Path, pattern: str) -> list:
    all_image_paths = list(data_root.glob(pattern))
    all_image_paths = [str(path) for path in all_image_paths]
    return all_image_paths


def count_image(data_root: Path, pattern: str) -> int:
    all_image_paths = get_all_image_paths(data_root, pattern)
    image_count = len(all_image_paths)
    return image_count


def preprocess_image(image):
    image = tf.image.decode_image(image, 3, tf.float32)
    image = tf.image.resize(image, [192, 192])
    return image


def load_and_preprocess_image(path):
    image = tf.io.read_file(path)
    return preprocess_image(image)
